home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / common / sources.c / dialogutilities.c next >
Encoding:
Text File  |  1996-09-26  |  21.5 KB  |  1,098 lines

  1. /*
  2.     File: DialogUtilities.c
  3.  
  4.     Copyright 1993-6 by Adobe Systems, Inc.
  5.  
  6.     C source file for plug-in dialog utilities.
  7. */
  8.  
  9. #include "DialogUtilities.h"
  10.  
  11. /*****************************************************************************/
  12.  
  13. /* The following routine locates the QuickDraw globals. */
  14.  
  15. QDGlobals *GetQDGlobals (void)
  16.     {
  17.     
  18.     return (QDGlobals *) ((* (char **) SetCurrentA5 ()) -
  19.                                     (sizeof (QDGlobals) - sizeof (GrafPtr)));
  20.     
  21.     }
  22.  
  23. /*****************************************************************************/
  24.  
  25. /* Set the cursor to the arrow cursor. */
  26.  
  27. void SetArrowCursor ()
  28.     {
  29.     
  30.     QDGlobals *qd = GetQDGlobals ();
  31.     
  32.     SetCursor (&(qd->arrow));
  33.     
  34.     }
  35.  
  36. /*****************************************************************************/
  37.  
  38. /* Centers a dialog template 1/3 of the way down on the main screen. */
  39. void CenterDialog (DialogTHndl dt)
  40.     {
  41.  
  42.     #define MenuHeight 20
  43.  
  44.     Rect screenBounds = (*GetMainDevice())->gdRect;
  45.  
  46.     short width = screenBounds.right - screenBounds.left;
  47.     short height = screenBounds.bottom - screenBounds.top;
  48.  
  49.     Rect dialogBounds = (**dt).boundsRect;
  50.  
  51.     OffsetRect (&dialogBounds, -dialogBounds.left, -dialogBounds.top);
  52.     OffsetRect (&dialogBounds, (width - dialogBounds.right) / 2,
  53.                     (height - dialogBounds.bottom - MenuHeight) / 3 + MenuHeight);
  54.     (**dt).boundsRect = dialogBounds;
  55.     
  56.     #undef menuHeight
  57.  
  58.     }
  59.  
  60. /*****************************************************************************/
  61.  
  62. /* Test for Adobe Moveable Modal Dialog if running System 6 */
  63.  
  64. void SetUpMoveableModal (DialogTHndl dt, OSType hostSig)
  65.     {
  66.  
  67.     long    response = 0x0700;
  68.     
  69.     if (Gestalt (gestaltSystemVersion, &response) != noErr)
  70.         response = 0;
  71.     
  72.     if (response < 0x0700)
  73.         {    /* Prior to System 7... */
  74.  
  75.         Handle    windRes = nil;
  76.         
  77.         /* We don't have to release the resource, because opening the
  78.            dialog will do the right thing with it if it exists. */
  79.  
  80.         if (hostSig == '8BIM')
  81.             windRes = GetResource ('WDEF', PSmovableDBoxProc/16);
  82.             
  83.         if (windRes != NULL)
  84.             (*dt)->procID = PSmovableDBoxProc;
  85.         else
  86.             (*dt)->procID = dBoxProc;        /* not moveable, sorry */
  87.  
  88.         }
  89.         
  90.     }
  91.  
  92. /*****************************************************************************/
  93.  
  94. typedef struct ModalData
  95.     {
  96.     
  97.     long oldRefCon;
  98.     
  99.     ModalFilterProcPtr filter;
  100.     
  101.     ProcessEventProc processEvent;
  102.     
  103.     } ModalData;
  104.  
  105. /*****************************************************************************/
  106.  
  107. /* Some handy ASCII values for keystrokes */
  108.  
  109. #define RETURN    0x0D
  110. #define ENTER    0x03
  111. #define PERIOD    '.'
  112. #define ESCAPE    0x1B
  113. #define    TAB        '\t'
  114.  
  115. /*****************************************************************************/
  116.  
  117. /* Event filter to allow movable modal dialogs. */
  118.  
  119. static pascal Boolean DialogFilter (DialogPtr dp,
  120.                                     EventRecord *event,
  121.                                     short *item)
  122.     {
  123.  
  124.     Boolean result = FALSE;
  125.     GrafPtr savePort;
  126.     
  127.     ModalData *data = (ModalData *) GetWRefCon (dp);
  128.     
  129.     if (data->filter)
  130.         {
  131.         
  132.         SetWRefCon (dp, data->oldRefCon);
  133.         
  134.         result = (*data->filter) (dp, event, item);
  135.         
  136.         data->oldRefCon = GetWRefCon (dp);
  137.         
  138.         SetWRefCon (dp, (long) data);
  139.         
  140.         if (result)
  141.             return TRUE;
  142.         
  143.         }
  144.     
  145.     if (event->what == mouseDown)
  146.         {
  147.  
  148.         /* We have to do window-dragging ourselves */
  149.  
  150.         Point pt = event->where;
  151.         WindowPtr window;
  152.  
  153.         if (FindWindow (pt, &window) == inDrag && window == dp)
  154.             {
  155.  
  156.             Rect bounds = (*GetGrayRgn())->rgnBBox;
  157.  
  158.             InsetRect (&bounds, 4, 4);
  159.  
  160.             DragWindow (window, pt, &bounds);
  161.  
  162.             event->what = nullEvent;
  163.  
  164.             }
  165.         }
  166.  
  167.     else if (event->what == keyDown && FALSE) // don't do this
  168.  
  169.         {
  170.  
  171.         /* We have to do button key-equivalents ourselves, now, too */
  172.  
  173.         char ch = event->message & charCodeMask;
  174.  
  175.         if (ch == RETURN || ch == ENTER)
  176.             {
  177.  
  178.             *item = ok;
  179.  
  180.             FlashDialogButton (dp, ok);
  181.  
  182.             result = TRUE;
  183.  
  184.             }
  185.  
  186.         else if (ch == ESCAPE || (ch == PERIOD && (event->modifiers & cmdKey)))
  187.             {
  188.  
  189.             *item = cancel;
  190.  
  191.             FlashDialogButton (dp, cancel);
  192.  
  193.             result = TRUE;
  194.  
  195.             }
  196.         else if (ch == TAB && (event->modifiers & shiftKey))
  197.             {
  198.             
  199.             // select previous item
  200.             
  201.             }
  202.         }
  203.     
  204.     else if (event->what == updateEvt || event->what == activateEvt)
  205.         {
  206.  
  207.         /* Pass updates and activates out to the host */
  208.  
  209.         if (data->processEvent && (event->message != ((long) dp)))
  210.             (*data->processEvent) (event);
  211.  
  212.         }
  213.  
  214.     else if (event->what == nullEvent)
  215.         {
  216.  
  217.         /* Let the host idle */
  218.  
  219.         if (data->processEvent)
  220.             (*data->processEvent) (event);
  221.  
  222.         }
  223.         
  224.     GetPort (&savePort);
  225.     SetPort (dp);
  226.         
  227.     result = StdFilterProc (dp, event, item);
  228.     
  229.     SetPort (savePort);
  230.  
  231.     return result;
  232.  
  233.     }
  234.  
  235. /*****************************************************************************/
  236.  
  237. #ifdef __powerc
  238.  
  239. static RoutineDescriptor DialogFilterRDS =
  240. BUILD_ROUTINE_DESCRIPTOR(uppModalFilterProcInfo, &DialogFilter);
  241.  
  242. #define DialogFilterRD (&DialogFilterRDS)
  243.  
  244. #else
  245.  
  246. #define DialogFilterRD (&DialogFilter)
  247.  
  248. #endif
  249.  
  250. /*****************************************************************************/
  251.  
  252. void MoveableModalDialog (DialogPtr dp,
  253.                           ProcessEventProc processEvent,
  254.                           ModalFilterProcPtr filter,
  255.                           short *item)
  256.     {
  257.     
  258.     ModalData data;
  259.     
  260.     data.oldRefCon = GetWRefCon (dp);
  261.     data.filter = filter;
  262.     data.processEvent = processEvent;
  263.     
  264.     SetWRefCon (dp, (long) &data);
  265.     
  266.     ModalDialog (DialogFilterRD, item);
  267.     
  268.     SetWRefCon (dp, data.oldRefCon);
  269.     
  270.     }
  271.  
  272. /*****************************************************************************/
  273.  
  274. long GetMoveableWRefCon (DialogPtr dp)
  275.     {
  276.     
  277.     ModalData *data = (ModalData *) GetWRefCon (dp);
  278.     
  279.     return data->oldRefCon;
  280.     
  281.     }
  282.  
  283. /*****************************************************************************/
  284.  
  285. short ShowAlert (short alertID)
  286. {
  287.     
  288.     Handle alertHandle;
  289.     short result;
  290.     
  291.     alertHandle = GetResource ('ALRT', alertID);
  292.     HNoPurge (alertHandle);
  293.     
  294.     CenterDialog ((DialogTHndl) alertHandle);
  295.     
  296.     result = Alert (alertID, nil);
  297.     
  298.     HPurge (alertHandle);
  299.  
  300.     return result;
  301.     
  302. }
  303.  
  304. /*****************************************************************************/
  305.  
  306. short ShowVersionAlert (Handle hDllInstance,
  307.                         short alertID, 
  308.                         short stringID,
  309.                         Str255 versText1,
  310.                         Str255 versText2)
  311. {
  312.     
  313.     short             result = 0;
  314.     Handle            alertHandle = NULL;
  315.     Str255            ds = "";
  316.     StringHandle    h = 0;
  317.     
  318.     if (alertID)
  319.     {        
  320.         if (stringID > 0)
  321.         {
  322.             h = GetString(stringID);
  323.             if (h) 
  324.             {
  325.                 AppendString(ds, *h, 1, *h[0]);
  326.                 ReleaseResource((Handle)h);
  327.             }
  328.         }
  329.         
  330.         alertHandle = GetResource('ALRT', alertID);
  331.         HNoPurge (alertHandle);
  332.     
  333.         CenterDialog ((DialogTHndl) alertHandle);
  334.     
  335.         PIParamText(ds, NULL, versText1, versText2);
  336.         ParamText (ds, NULL, NULL, NULL);
  337.         
  338.         result = CautionAlert (alertID, nil);
  339.     
  340.         HPurge (alertHandle);
  341.     }
  342.     return result;
  343. }
  344.  
  345. /*****************************************************************************/
  346.  
  347. short ShowAlertType (Handle hDllInstance,
  348.                      short alertID, 
  349.                      short stringID, 
  350.                      Str255 minText, 
  351.                      Str255 maxText,
  352.                      short alertType)
  353. {
  354.     
  355.     short             result = 0;
  356.     Handle            alertHandle = NULL;
  357.     Str255            ds = "";
  358.     StringHandle    h = 0;
  359.     
  360.     if (alertID)
  361.     {        
  362.         if (stringID > 0)
  363.         {
  364.             h = GetString(stringID);
  365.             if (h) 
  366.             {
  367.                 AppendString(ds, *h, 1, *h[0]);
  368.                 ReleaseResource((Handle)h);
  369.             }
  370.         }
  371.         
  372.         alertHandle = GetResource('ALRT', alertID);
  373.         HNoPurge (alertHandle);
  374.     
  375.         CenterDialog ((DialogTHndl) alertHandle);
  376.     
  377.         PIParamText(ds, NULL, minText, maxText);
  378.         ParamText (ds, NULL, NULL, NULL);
  379.         
  380.         switch (alertType)
  381.         {
  382.             case PIAlertCaution:
  383.                 result = CautionAlert (alertID, nil);
  384.                 break;
  385.             case PIAlertStop:
  386.                 result = StopAlert (alertID, nil);
  387.                 break;
  388.         }
  389.     
  390.         HPurge (alertHandle);
  391.     }
  392.     return result;
  393. }
  394.  
  395. /*****************************************************************************/
  396.  
  397. void ShowAbout (OSType hostSign, short dialogID)
  398. {
  399.  
  400.     short             item;
  401.     DialogPtr         dp;
  402.     DialogTHndl     dt;
  403.     StringHandle    h;
  404.     
  405.     h = GetString(dialogID);
  406.     
  407.     dt = (DialogTHndl) GetResource ('DLOG', dialogID);
  408.     HNoPurge ((Handle) dt);
  409.  
  410.     CenterDialog (dt);
  411.  
  412.     SetUpMoveableModal (dt, hostSign);
  413.  
  414.     dp = GetNewDialog (dialogID, nil, (WindowPtr) -1);
  415.     
  416.     ParamText (*h, NULL, NULL, NULL);
  417.     
  418.     SetArrowCursor ();
  419.     
  420.     (void) SetDialogDefaultItem (dp, ok); // hidden
  421.     (void) SetDialogCancelItem (dp, ok); // either
  422.  
  423.     MoveableModalDialog (dp, NULL, NULL, &item);
  424.         
  425.     DisposDialog (dp);
  426.     ReleaseResource((Handle)h); // DisposeHandle(Handle(h))
  427.     HPurge ((Handle) dt);
  428.  
  429. }
  430.  
  431. /*****************************************************************************/
  432.  
  433. /* UserItem to outline a button group box, except for top text */
  434.  
  435. static pascal void OutlineGroup (DialogPtr dp, short item)
  436.     {
  437.  
  438.     Rect     r, rText;
  439.     Handle     h;
  440.     short     itemType;
  441.     PenState originalPenState;
  442.  
  443.     GetPenState (&originalPenState);
  444.  
  445.     GetDialogItem (dp, item+1, &itemType, &h, &rText);    /* bounds of
  446. surrounding text */
  447.     GetDialogItem (dp, item,   &itemType, &h, &r);        /* the group box */
  448.     
  449.     PenNormal ();
  450.     PenSize (1, 1);
  451.     
  452.     MoveTo (rText.right, r.top);
  453.     LineTo (r.right, r.top);
  454.     LineTo (r.right, r.bottom);
  455.     LineTo (r.left, r.bottom);
  456.     LineTo (r.left, r.top);
  457.     LineTo (rText.left, r.top);
  458.     
  459.     SetPenState (&originalPenState);
  460.     
  461.     }
  462.  
  463. /*****************************************************************************/
  464.  
  465. #ifdef __powerc
  466.  
  467. static RoutineDescriptor OutlineGroupRDS =
  468.     BUILD_ROUTINE_DESCRIPTOR(uppUserItemProcInfo, &OutlineGroup);
  469.  
  470. #define OutlineGroupRD (&OutlineGroupRDS)
  471.  
  472. #else
  473.  
  474. #define OutlineGroupRD (&OutlineGroup)
  475.  
  476. #endif
  477.  
  478. /*****************************************************************************/
  479.  
  480. /* The following routine sets a user item to be a group box.  It expects
  481.    the next item to be the title for the group box. */
  482.  
  483. void SetOutlineGroup (DialogPtr dp, short groupItem)
  484.     {
  485.     
  486.     short itemType;
  487.     Rect r;
  488.     Handle h;
  489.  
  490.     GetDialogItem (dp, groupItem, &itemType, &h                     , &r);
  491.     SetDialogItem (dp, groupItem,  itemType, (Handle) OutlineGroupRD, &r);
  492.     
  493.     }
  494.  
  495. /*****************************************************************************/
  496.  
  497. /* The following routine selects an edit text item. */
  498.  
  499. void SelectTextItem (DialogPtr dp, short item)
  500.     {
  501.     
  502.     SelIText (dp, item, 0, 32767);
  503.     
  504.     }
  505.  
  506. /*****************************************************************************/
  507.  
  508. /* The following routine sets the text of a text item. */
  509.  
  510. void StuffText (DialogPtr dp, short item, Str255 text)
  511.     {
  512.     
  513.     Rect r;
  514.     short itemType;
  515.     Handle textHdl;
  516.     
  517.     GetDialogItem (dp, item, &itemType, &textHdl, &r);
  518.     
  519.     SetDialogItemText (textHdl, text);
  520.     
  521.     }
  522.  
  523. /*****************************************************************************/
  524.  
  525. /* The following routine extracts the text of a text item. */
  526.  
  527. void FetchText (DialogPtr dp, short item, Str255 text)
  528.     {
  529.     
  530.     Rect r;
  531.     short itemType;
  532.     Handle textHdl;
  533.     
  534.     GetDialogItem (dp, item, &itemType, &textHdl, &r);
  535.     
  536.     GetDialogItemText (textHdl, text);
  537.     
  538.     }
  539.     
  540. /*****************************************************************************/
  541.  
  542. /* The following routine stuffs a numeric value into a text field. */
  543.  
  544. void StuffNumber (DialogPtr dp, short item, long value)
  545.     {
  546.     
  547.     Str255 s;
  548.         
  549.     NumToString (value, s);
  550.     
  551.     StuffText (dp, item, s);
  552.     
  553.     }
  554.  
  555. /*****************************************************************************/
  556.  
  557. /* 
  558.    Here is the corresponding routine to retrieve the value from a text
  559.    field.  It will do range checking and validate that it has been
  560.    handed a number. 
  561.    
  562.    It returns TRUE if it gets a valid value in the field. */
  563.    
  564. short FetchNumber (DialogPtr dp,
  565.                     short item,
  566.                     long min,
  567.                     long max,
  568.                     long *value)
  569. {
  570.         
  571.     Str255 s = "";
  572.     long x = 0;
  573.     short retn = noErr;
  574.     
  575.     FetchText (dp, item, s);
  576.     
  577.     if (!StringToNumber (s, &x))
  578.     {
  579.         x = 0;
  580.         retn = errNotANumber;
  581.     }
  582.     else if (x < min || x > max) retn = errOutOfRange;
  583.     
  584.     *value = x;
  585.     return retn;
  586. }
  587.  
  588. /*****************************************************************************/
  589.  
  590. /* Here is the accompanying alert mechanism for a text field.
  591.    If it has not been handed a number, it brings up
  592.    an appropriate error dialog, inserts an appropriately pinned value,
  593.    and selects the item.
  594.   
  595.    It returns TRUE if it gets a valid value in the field. */
  596.  
  597. void AlertNumber (DialogPtr dp,
  598.                   short item,
  599.                   long min,
  600.                   long max,
  601.                   long *value,
  602.                   Handle hDllInstance,
  603.                    short alertID,
  604.                    short numberErr)
  605. {
  606.     Str255 minText = "";
  607.     Str255 maxText = "";
  608.     long x = *value;
  609.     
  610.     x = (x < min ? min : max);
  611.     *value = x;    
  612.     StuffNumber (dp, item, x);
  613.  
  614.     NumToString(min, minText);
  615.     NumToString(max, maxText);
  616.     
  617.     (void) ShowCaution (hDllInstance,
  618.                         alertID,
  619.                           kBadNumberID, // could use numberErr==errNotANumber
  620.                         minText,      // to pop a "that's not a number"
  621.                         maxText);      // alert.        
  622.  
  623.     SelectTextItem (dp, item);
  624. }
  625.  
  626.  
  627. /*****************************************************************************/
  628. /* The following routine stuffs a double into a text field. */
  629.  
  630. void StuffDouble (DialogPtr dp, short item, double value, short precision)
  631. {
  632.     Str255     s;
  633.     DoubleToString(value, s, precision);
  634.     StuffText(dp, item, s);
  635. }
  636.  
  637. /*****************************************************************************/
  638.  
  639. /* 
  640.    Here is the corresponding routine to retrieve the floating value from a text
  641.    field.  It will do range checking and validate that it has been
  642.    handed a number.
  643.    
  644.    It returns noErr if it gets a valid value. */
  645.    
  646. short FetchDouble (DialogPtr dp,
  647.                     short item,
  648.                     double min,
  649.                     double max,
  650.                     double *value)
  651. {
  652.     Str255 s1 = "";
  653.     Str255 s2 = "";
  654.         
  655.     long x1 = 0;
  656.     long x2 = 0;
  657.     short precision = 0;
  658.     Boolean notAWholeNumber = false;
  659.     Boolean notADecimalNumber = false;
  660.     Boolean notANumber = false;
  661.     double x = 0;
  662.     short retn = noErr;
  663.     
  664.     FetchText (dp, item, s1);
  665.     
  666.     DivideAtDecimal(s1, s2);
  667.     
  668.     notAWholeNumber = !StringToNumber (s1, &x1);
  669.     
  670.     notADecimalNumber = !StringToNumber (s2, &x2);
  671.     
  672.     precision = s2[0]; //length
  673.     
  674.     notANumber = (notAWholeNumber && notADecimalNumber);
  675.     
  676.     x = (double)x1 + ((double)x2 / (double)power(10, s2[0]));
  677.     
  678.     if (notANumber)
  679.     {
  680.         x = 0;
  681.         retn = errNotANumber;
  682.     }
  683.     else if (x < min || x > max) retn = errOutOfRange;
  684.     
  685.     *value = x;
  686.     return retn;
  687. }
  688.  
  689.  
  690. /*****************************************************************************/
  691.  
  692. /*
  693.      If it has not been handed a number, it brings up
  694.     an appropriate error dialog, inserts an appropriately pinned value,
  695.     and selects the item. */
  696.   
  697. void AlertDouble (DialogPtr dp,
  698.                  short item,
  699.                  double min,
  700.                  double max,
  701.                  double *value,
  702.                  Handle hDllInstance,
  703.                  short alertID,
  704.                  short numberErr)
  705.     Str255 minText = "";
  706.     Str255 maxText = "";
  707.     short precision = 0;
  708.     double x = *value;
  709.     
  710.     // Figure out precision
  711.     FetchText(dp, item, minText);
  712.     DivideAtDecimal(minText, maxText);
  713.     precision = maxText[0]; // no more than number of digits in decimal
  714.     
  715.     x = (x < min ? min : max);
  716.     *value = x;        
  717.     StuffDouble (dp, item, x, precision);
  718.  
  719.  
  720.     DoubleToString (min, minText, precision);
  721.     DoubleToString (max, maxText, precision);
  722.     
  723.     (void) ShowCaution (hDllInstance,
  724.                         alertID,
  725.                           kBadDoubleID, // could use numberErr==errNotANumber
  726.                         minText,      // to pop a "that's not a number"
  727.                         maxText);      // alert.    
  728.  
  729.     SelectTextItem (dp, item);
  730. }
  731.  
  732. /*****************************************************************************/
  733.  
  734. /* Set the state of a check box (or radio button). */
  735.  
  736. void SetCheckBoxState (DialogPtr dp, short item, Boolean checkIt)
  737.     {
  738.  
  739.     Rect    r;
  740.     Handle    ctl;
  741.     short    itemType;
  742.     short    oldValue, newValue;
  743.     
  744.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  745.  
  746.     oldValue =  GetCtlValue ((ControlHandle) ctl);
  747.     
  748.     newValue = checkIt ? 1 : 0;
  749.  
  750.     if (oldValue != newValue)
  751.         SetCtlValue ((ControlHandle) ctl, newValue);
  752.  
  753.     }
  754.  
  755. /*****************************************************************************/
  756.  
  757. /* Determine the state of a check box (or radio button). */
  758.  
  759. Boolean GetCheckBoxState (DialogPtr dp, short item)
  760.     {
  761.     
  762.     Rect    r;
  763.     Handle    ctl;
  764.     short    itemType;
  765.     short    oldValue;
  766.     
  767.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  768.  
  769.     oldValue = GetCtlValue ((ControlHandle) ctl);
  770.     
  771.     return (oldValue != 0);
  772.     
  773.     }
  774.  
  775. /*****************************************************************************/
  776.  
  777. /* Toggle a check box and return the new state. */
  778.  
  779. Boolean ToggleCheckBoxState (DialogPtr dp, short item)
  780.     {
  781.     
  782.     Boolean newState = !GetCheckBoxState (dp, item);
  783.     
  784.     SetCheckBoxState (dp, item, newState);
  785.     
  786.     return newState;
  787.     
  788.     }
  789.  
  790. /*****************************************************************************/
  791.  
  792. /* Set a radio group (from first to last item) to reflect the selection. */
  793.  
  794. void SetRadioGroupState (DialogPtr dp,
  795.                          short first,
  796.                          short last,
  797.                          short item)
  798.     {
  799.  
  800.     short            i;
  801.     
  802.     for (i = first; i <= last; ++i)
  803.         SetCheckBoxState (dp, i, i == item);
  804.  
  805.     }
  806.  
  807. /*****************************************************************************/
  808.  
  809. /* Get the selected radio button in a group. */
  810.  
  811. short GetRadioGroupState (DialogPtr dp, short first, short last)
  812.     {
  813.     
  814.     short i;
  815.     
  816.     for (i = first; i <= last; ++i)
  817.         if (GetCheckBoxState (dp, i))
  818.             return i;
  819.             
  820.     return 0;
  821.     
  822.     }
  823.  
  824. /*****************************************************************************/
  825.  
  826. /* Set the state of a pop-up menu. */
  827.  
  828. void SetPopUpMenuValue (DialogPtr dp, short item, short newValue)
  829.     {
  830.  
  831.     Rect    r;
  832.     Handle    ctl;
  833.     short    itemType;
  834.     short    oldValue;
  835.     
  836.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  837.  
  838.     oldValue =  GetCtlValue ((ControlHandle) ctl);
  839.     
  840.     if (oldValue != newValue)
  841.         {
  842.         if (GetCtlMax ((ControlHandle) ctl) < newValue)
  843.             SetCtlMax ((ControlHandle) ctl, newValue);
  844.         SetCtlValue ((ControlHandle) ctl, newValue);
  845.         }
  846.  
  847.     }
  848.  
  849. /*****************************************************************************/
  850.  
  851. /* Determine the state of a pop-up menu. */
  852.  
  853. short GetPopUpMenuValue (DialogPtr dp, short item)
  854.     {
  855.     
  856.     Rect    r;
  857.     Handle    ctl;
  858.     short    itemType;
  859.     
  860.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  861.  
  862.     return GetCtlValue ((ControlHandle) ctl);
  863.     
  864.     }
  865.  
  866. /*****************************************************************************/
  867.  
  868. /* Utility routine to hide or enable an item. */
  869.  
  870. void ShowHideItem (DialogPtr dp, short item, Boolean state)
  871. {
  872.     if (state)
  873.         ShowDialogItem(dp, item);
  874.     else
  875.         HideDialogItem(dp, item);
  876. }
  877.  
  878.  
  879. /*****************************************************************************/
  880.  
  881. /* Utility routine to disable a control. */
  882.  
  883. void DisableControl (DialogPtr dp, short item)
  884.     {
  885.  
  886.     Rect    r;
  887.     Handle    ctl;
  888.     short    itemType;
  889.     short    oldValue;
  890.     
  891.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  892.  
  893.     itemType |= itemDisable;
  894.  
  895.     oldValue = GetCtlValue ((ControlHandle) ctl);
  896.     if (oldValue != 0)
  897.         SetCtlValue ((ControlHandle) ctl, 0);
  898.  
  899.     HiliteControl ((ControlHandle) ctl, 255);
  900.  
  901.     SetDialogItem (dp, item, itemType, ctl, &r);
  902.  
  903.     }
  904.  
  905. /*****************************************************************************/
  906.  
  907. /* Utility routine to enable a control. */
  908.  
  909. void EnableControl (DialogPtr dp, short item)
  910.     {
  911.  
  912.     Rect    r;
  913.     Handle    ctl;
  914.     short    itemType;
  915.     
  916.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  917.  
  918.     itemType &= ~itemDisable;
  919.  
  920.     HiliteControl ((ControlHandle) ctl, 0);
  921.  
  922.     SetDialogItem (dp, item, itemType, ctl, &r);
  923.  
  924.     }
  925.  
  926. /*****************************************************************************/
  927.  
  928. /* Utility routine to enable a control. */
  929.  
  930. void EnableDisableControl (DialogPtr dp, short item, Boolean state)
  931.     {
  932.     
  933.     if (state)
  934.         EnableControl (dp, item);
  935.     else
  936.         DisableControl (dp, item);
  937.     
  938.     }
  939.  
  940. /*****************************************************************************/
  941.  
  942. /* Utility routine to invalidate an item. */
  943.  
  944. void InvalItem (DialogPtr dp, short item)
  945.     {
  946.     
  947.     Rect    r;
  948.     Handle    h;
  949.     short    itemType;
  950.     GrafPtr oldPort;
  951.     
  952.     GetDialogItem (dp, item, &itemType, &h, &r);
  953.     
  954.     GetPort (&oldPort);
  955.     
  956.     SetPort (dp);
  957.     
  958.     InvalRect (&r);
  959.     
  960.     SetPort (oldPort);
  961.     
  962.     }
  963.  
  964. /*****************************************************************************/
  965.  
  966. /* Perform standard handling for check boxes and radio buttons. For radio
  967.    buttons, we assume that the group for the radio button extends forward
  968.    and backward in the DITL as long as the item type is radio button. */
  969.    
  970. void PerformStandardDialogItemHandling (DialogPtr dp, short item)
  971.     {
  972.     
  973.     Rect r;
  974.     Handle h;
  975.     short itemType;
  976.     
  977.     short first, last, count;
  978.     
  979.     GetDialogItem (dp, item, &itemType, &h, &r);
  980.     
  981.     switch (itemType)
  982.         {
  983.         
  984.         case ctrlItem+chkCtrl:
  985.             (void) ToggleCheckBoxState (dp, item);
  986.             break;
  987.             
  988.         case ctrlItem+radCtrl:
  989.             first = last = item;
  990.             while (first > 1)
  991.                 {
  992.                 GetDialogItem (dp, first - 1, &itemType, &h, &r);
  993.                 if (itemType != ctrlItem+radCtrl)
  994.                     break;
  995.                 --first;
  996.                 }
  997.             count = CountDITL (dp);
  998.             while (last < count)
  999.                 {
  1000.                 GetDialogItem (dp, last + 1, &itemType, &h, &r);
  1001.                 if (itemType != ctrlItem+radCtrl)
  1002.                     break;
  1003.                 ++last;
  1004.                 }
  1005.             SetRadioGroupState (dp, first, last, item);
  1006.             break;
  1007.         
  1008.         }
  1009.     
  1010.     }
  1011.  
  1012. /*****************************************************************************/
  1013.  
  1014. /* Little routine to flash a button set off by a keystroke. */
  1015.  
  1016.  void FlashDialogButton (DialogPtr dialog,short  item)
  1017.     {
  1018.  
  1019.     short itemType;
  1020.     Handle h;
  1021.     Rect r;
  1022.  
  1023.     GetDItem (dialog, item, &itemType, &h, &r);
  1024.     
  1025.     if (itemType == (ctrlItem + btnCtrl))
  1026.         {
  1027.  
  1028.         long ignore;
  1029.  
  1030.         HiliteControl ((ControlHandle) h, 1);
  1031.  
  1032.         Delay (5, &ignore);
  1033.  
  1034.         HiliteControl ((ControlHandle) h, 0);
  1035.  
  1036.         }
  1037.     
  1038.  
  1039.     }
  1040.  
  1041. /*****************************************************************************/
  1042.  
  1043. /* UserItem to outline the OK button in a dialog box. */
  1044.  
  1045. static pascal void OutlineOK (DialogPtr dp, short item)
  1046.     {
  1047.     
  1048.     PenState originalPenState;
  1049.  
  1050.     Rect r;
  1051.     Handle h;
  1052.     short itemType;
  1053.     
  1054.     GetPenState (&originalPenState);
  1055.  
  1056.     item = ok;
  1057.  
  1058.     GetDItem (dp, item, &itemType, &h, &r);
  1059.  
  1060.     PenNormal ();
  1061.     PenSize (3, 3);
  1062.     
  1063.     InsetRect (&r, -4, -4);
  1064.     FrameRoundRect (&r, 16, 16);
  1065.  
  1066.     SetPenState (&originalPenState);
  1067.     
  1068.     }
  1069.  
  1070. /*****************************************************************************/
  1071.  
  1072. #ifdef __powerc
  1073.  
  1074. static RoutineDescriptor OutlineOKRDS =
  1075.     BUILD_ROUTINE_DESCRIPTOR(uppUserItemProcInfo, &OutlineOK);
  1076.  
  1077. #define OutlineOKRD (&OutlineOKRDS)
  1078.  
  1079. #else
  1080.  
  1081. #define OutlineOKRD (&OutlineOK)
  1082.  
  1083. #endif
  1084.  
  1085. /*****************************************************************************/
  1086.  
  1087. void SetOutlineOKHook (DialogPtr dp, short hookItem)
  1088.     {
  1089.     
  1090.     short itemType;
  1091.     Rect r;
  1092.     Handle h;
  1093.  
  1094.     GetDItem (dp, hookItem, &itemType, &h                  , &r);
  1095.     SetDItem (dp, hookItem,  itemType, (Handle) OutlineOKRD, &r);
  1096.     
  1097.     }